package com.yqg.education.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.entity.Live;
import com.yqg.education.mapper.LivePlaybackMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;

import com.yqg.education.service.ILivePlaybackService;
import com.yqg.education.entity.LivePlayback;

import org.springframework.web.bind.annotation.RestController;

/**
 * LivePlaybackController 前端控制器
 *
 * @author zxf
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/live-playback")
public class LivePlaybackController {

    @Resource
    private ILivePlaybackService livePlaybackService;

    @Resource
    private LivePlaybackMapper livePlaybackMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody LivePlayback livePlayback) {
        return Result.success(livePlaybackService.saveOrUpdate(livePlayback));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        LivePlayback livePlayback = livePlaybackService.getById(id);
        livePlayback.setIsDelete(1);
        return Result.success(livePlaybackService.saveOrUpdate(livePlayback));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<LivePlayback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<LivePlayback> list = livePlaybackService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(livePlaybackService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Long pageNum,
                           @RequestParam Long pageSize,
                           @RequestParam(required = false) Long courseId,
                           @RequestParam(required = false) Long teachId) {
        return Result.success(livePlaybackService.findPage(pageNum, pageSize, courseId, teachId));
    }

    /**
     * 学员端分页查询
     */
    @GetMapping("/stuPage")
    public Result stuFindPage(@RequestParam Long pageNum,
                              @RequestParam Long pageSize,
                              @RequestParam(required = false) Long courseId,
                              @RequestParam(required = false) Long teachId,
                              @RequestParam Long stuId) {
        return Result.success(livePlaybackService.stuFindPage(pageNum, pageSize, courseId, teachId,stuId));
    }
    /**
     * 通过liveId查询回放
     */
    @GetMapping("/getLivePlaybackByLiveId")
    public Result getLivePlaybackByLiveId(@RequestParam Long pageNum,
                                          @RequestParam Long pageSize,
                                          @RequestParam Long liveId) {
        Page<LivePlayback> page = new Page<>(pageNum, pageSize);

        QueryWrapper<LivePlayback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("live_id", liveId)
                .eq("is_delete", 0);

        Page<LivePlayback> resultPage = livePlaybackService.page(page, queryWrapper);

        return Result.success(resultPage);
    }
}

